{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Python基础教程  异常处理"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "相信在C++中，一定接触过try-except等代码块\n",
    "Python中也有异常处理机制，为try-except-finally体系"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "除法运行错误，请检查数值\n"
     ]
    }
   ],
   "source": [
    "#捕获异常\n",
    "try:\n",
    "    num1 = 10\n",
    "    num2 = 0\n",
    "    print(num1 / num2)\n",
    "except:\n",
    "    print(\"除法运行错误，请检查数值\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I/O error: [Errno 2] No such file or directory: 'myfile.txt'\n"
     ]
    }
   ],
   "source": [
    "#捕获多种异常\n",
    "import sys\n",
    "\n",
    "try:\n",
    "    with open('myfile.txt') as files:\n",
    "        s = files.readline()\n",
    "except IOError as err:\n",
    "    print(\"I/O error: {0}\".format(err))\n",
    "except ValueError:\n",
    "    print(\"Could not convert data to an integer.\")\n",
    "except:\n",
    "    print(\"Unexpected error:\", sys.exc_info()[0])\n",
    "    raise"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "异常类型是一个class，一定要把基类放在下面"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "def demo():\n",
    "  try:\n",
    "    with open(\"test.txt\") as myfile:\n",
    "        myfile.read()\n",
    "\n",
    "  except Exception as e:\n",
    "    print(\"程序运行异常\", e)\n",
    "  except IOError as e:\n",
    "    print(\"IO异常\", e)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "程序运行异常 [Errno 2] No such file or directory: 'test.txt'\n"
     ]
    }
   ],
   "source": [
    "demo()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "程序异常 division by zero\n",
      "Over\n"
     ]
    }
   ],
   "source": [
    "#清理异常\n",
    "#即finally语句\n",
    "try:\n",
    "    num1=10\n",
    "    num2=0\n",
    "    print(num1/num2)\n",
    "except Exception as e:\n",
    "    print(\"程序异常\",e)\n",
    "finally:\n",
    "    print(\"Over\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "#finally关键字仅能出现一次"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}